const Mock = require('mockjs')


const myOrderData = Mock.mock({
    'data|12': [{
      'orderId|+1': 1,
      'userId|+1': "@integer(1, 30)",
      'businessId|+1': "@integer(1, 20)",
      'orderDate': "@date",
      'orderTotal': '@float(10, 100,0,2)',
      'daId': "@integer(1,100)",
      'orderState':"@integer(1, 2)"
    }]
  
  })

  module.exports = [

    //获取订单列表
  {
    url: '/vue-admin-template/orders/list',
    type: 'get',
    response: config => {

      const params = config.query;
      // console.log("------mock food data:%o", myOrderData.data);
      console.log("-----订单列表接口，请求参数:%o", params);

      const fId = (undefined == params.orderId || params.orderId.trim() == "") ? null : params.orderId.trim();
      const fName = (undefined == params.userId || params.userId.trim() == "") ? null : params.userId.trim();

      const pageNo = +params.page; //页码
      const pageSize = +params.limit; //每页大小
      let retData = [];

      // 处理：非分页的查询参数
      if (fId != null || fName != null) {
        for (let x of myOrderData.data) {
          // console.log("-----fId:%d,fName:%s,x:%o,fId ==orderId:%s", fId, fName, x, (fId == x.orderId) + "");

          // 1.只输入：订单编号
          if ((fId != null && fId == x.orderId) && (fName == null)) {
            retData.push(x);
          }
          console.log(typeof x.userId);
          x.userId=x.userId.toString()
          // 2.只输入：订单名称
          if ((fId == null) &&
            (fName != null && x.userId.indexOf(fName) != -1)) {
            retData.push(x);

          }
          // 3.输入的是：订单编号和订单名称
          if ((fId != null && fId == x.orderId) &&
            (fName != null && x.userId.indexOf(fName) != -1)) {
            retData.push(x);

          }
        }
      } else {
        retData = myOrderData.data;
      }
      // 处理：分页问题（将查询出来的结果，按分页）
      let startIndex = (pageNo - 1) * pageSize;
      let endIndex = startIndex + pageSize;
      console.log(`---------startIndex:${startIndex},endIndex:${endIndex}`);
      retData = retData.slice(startIndex, endIndex);

      return {
        code: 20000,
        data: {
          "data": retData,
          "total": 12
        }
      }
    }
  },

  //根据订单id获取订单信息
  {
    url: '/vue-admin-template/orders/detail',
    type: 'get',
    response: config => {
      const params = config.query;
      console.log("-----订单列表接口，请求参数:%o", params);
      let retData = null;
      myOrderData.data.forEach(x => {
        if (x.orderId == params.id) {
          retData = x;
        }
      });
      return {
        code: 20000,
        data: {
         "orders":retData
        }
      }
    }
  },
   //更新订单信息
 {
  url: '/vue-admin-template/orders/update',
  type: 'post',
  response: config => {
  let params=config.body;
  console.log("-----更新订单接口，请求参数:%o", params);  
  myOrderData.data.forEach((item,index) => {
    // console.log(item.orderId);
      if (item.orderId == params.orders.orderId) {         
          orderIndex=index;
          item.orderId=params.orders.orderId;
          item.userId=params.orders.userId;
          item.businessId=params.orders.businessId;
          item.orderDate=params.orders.orderDate;
          item.orderTotal=params.orders.orderTotal;
          item.daId=params.orders.daId;
          item.orderState=params.orders.businessId;
      }        
    });
       
    return {
      code: 20000,
      data:"更新成功"
    }
  }
},
  //根据id删除订单信息
  {
    url: '/vue-admin-template/orders/delete',
    type: 'post',
    response: config => {
        const params = config.body;
        console.log("-----订单列表接口，请求参数:%o", params);
        let orderIndex = -1; //订单数组中的订单索引
        myOrderData.data.forEach((x,index) => {
          if (x.orderId == params.orderId) {
            orderIndex = index;
          }
        });
        //将该订单信息从订单列表中删除
        myOrderData.data.splice(orderIndex,1);

        return {
          code: 20000,
          data: "删除订单成功！"
        }
      }
  },

  ]